import addCar    from "./g_addCar.js";
function Cars(){}
$.extend(Cars.prototype,{
    init:function(data){
        this.btns = $(".add_box");
        this.id  = data.dataid
        console.log(this.id)
         this.btns.on("click",$.proxy(function(){
            this.input.val(1);
            this.number = this.input.val();
        },this))
        this.btns.on("click",$.proxy(function(){
            var number = parseInt($(".txts").val());
            this.addLS(number)
            addCar.init(1);
        },this))
    },
    addLS:function(number){
        number = parseInt($(".txts").val());
        var s = localStorage.getItem("carts");
        if( s == null){
            var a =[{
                id : this.id,
                count : number
            }]
            localStorage.setItem("carts",JSON.stringify(a));
        }else{
            var list = JSON.parse(s)
            var flag = false;
            $.each(list,$.proxy(function(index,item){
                if(item.id === this.id){
                   var num = Number(item.count);
                     item.count = (num + number);
                   flag = true;
                }
            },this))
            if(!flag){
                list.push({
                    id : this.id,
                    count : number
                })
            }
            localStorage.setItem("carts",JSON.stringify(list));
        }
    }
})
export default new Cars();